#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 7e5 + 10;
const int V = 233233233;

tuple < int, int, int, int > line[N];

namespace dsu {
	int fa[N << 5], cnt;
	int nd() { return ++cnt, fa[cnt] = cnt, cnt; }
	int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
	void merge(int x, int y) { fa[gf(x)] = gf(y); }
}

ll ans;

mt19937 rnd(233233233);

namespace treap {
	int siz[N], cnt[N], val[N], f[N], ch[N][2], pri[N], ndn, rt;
	bool tg[N];
	int nd(int x) { ++ndn; val[ndn] = x; siz[ndn] = 1; pri[ndn] = rnd() % V; return ndn; }
	void pt(int x) { cnt[x] = siz[x]; tg[x] = true; }
	void pd(int x) { if(tg[x]) { f[x] = dsu :: nd(); pt(ch[x][0]), pt(ch[x][1]); tg[x] = 0; cnt[x]--; } }
	void pu(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; cnt[x] = cnt[ch[x][0]] + cnt[ch[x][1]]; }
	int merge(int x, int y) {
		if(!x || !y) return x | y; pd(x); pd(y);
		if(pri[x] < pri[y]) return ch[x][1] = merge(ch[x][1], y), pu(x), x;
		else return ch[y][0] = merge(x, ch[y][0]), pu(y), y;
	}
	int a, b, c, d, e; 
	void split(int cur, int k, int &x, int &y) {
		if(!cur) return x = y = 0, void(); pd(cur);
		if(val[cur] <= k) return x = cur, split(ch[x][1], k, ch[x][1], y), pu(x);
		else return y = cur, split(ch[y][0], k, x, ch[y][0]), pu(y);
	}
	int rig(int x) { while(ch[x][1]) pd(x), x = ch[x][1]; return x; }
	void topins(int x) { split(rt, x, a, b); c = nd(x); f[c] = f[rig(a)]; rt = merge(merge(a, c), b);	}
	void potins(int x) { split(rt, x - 1, a, b); split(b, x, b, c); dsu :: merge(f[rig(a)], f[b]); rt = merge(a, c); }
	void linins(int l, int r) {
		split(rt, l - 1, a, b); split(b, r, b, c); d = rig(b); split(b, val[d] - 1, b, d);
		ans += cnt[b]; pt(b); rt = merge(merge(a, b), merge(d, c));
	}
}

int W, H, n, cnt;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    W = in, H = in, n = in;
	rep(i, 1, n) {
		int a = in, b = in, c = in, d = in;
		if(b == d) line[++cnt] = make_tuple(b, 1, a, c);
		else line[++cnt] = make_tuple(b, 0, a, 0), line[++cnt] = make_tuple(d, 2, a, 0);
	}
	line[++cnt] = make_tuple(0, 0, 0, 0); line[++cnt] = make_tuple(0, 0, W, 0);
	line[++cnt] = make_tuple(0, 1, 0, W), line[++cnt] = make_tuple(H, 1, 0, W);
	line[++cnt] = make_tuple(H, 2, 0, 0); line[++cnt] = make_tuple(H, 2, W, 0);
	sort(line + 1, line + cnt + 1);
	treap :: f[treap :: rt = treap :: nd(-2e9)] = dsu :: nd();
	rep(i, 1, cnt) {
		if(get<1>(line[i]) == 0) treap :: topins(get<2>(line[i]));
		else if(get<1>(line[i]) == 1) treap :: linins(get<2>(line[i]), get<3>(line[i]));
		else treap :: potins(get<2>(line[i]));
	} rep(i, 1, dsu :: cnt) if(dsu :: fa[i] == i) ans++;
	cout << ans - 1 << endl; return 0;
}
